package cn.audit.controller.zqmk;

import cn.audit.bean.pojo.Config;
import cn.audit.bean.pojo.Property;
import cn.audit.repository.zqmk.PropertyRepository;
import cn.audit.service.zqmk.ConfigService;
import cn.audit.service.zqmk.PropertyListService;
import cn.audit.service.zqmk.PropertyService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 钟琪
 * @Date: 2019/5/23 14:15
 */
@Controller
@Transactional
public class AttributeSetController {
    @Resource
    private ConfigService configService;
    @Resource
    private PropertyService propertyService;
    @Resource
    private PropertyRepository propertyRepository;
    @Resource
    private PropertyListService propertyListService;

    @RequestMapping("/AttributeSet")
    public String AttributeSet(Model model){
        List<Config> configList= configService.findAllByConfigCode("object");
        model.addAttribute("configlist",configList);
        return "/AttributeSet";
    }
    @ResponseBody
    @RequestMapping("/configproperty")
    public List<Property> configproperty(Integer id){
        System.out.println(id);
        List<Property> propertyList=propertyService.findAllByConfigId(id);
        System.out.println(propertyList);
        return propertyList;
    }
    @RequestMapping("/propertysavename")
    public String propertysavename(Property property){
        propertyRepository.save(property);
        return "redirect:/AttributeSet";

    }
    @ResponseBody
    @RequestMapping("/selectAllById")
    public Property selectAllById(Integer id){
        Optional<Property> propertyOptional=propertyRepository.findById(id);
        Property property=propertyOptional.get();
        System.out.println(property);
        return property;
    }
    @ResponseBody
    @RequestMapping("/updateByAll")
    public Property updateByAll(Property property){
        System.out.println(property+"44444444444444444444444444444444444444");
        Optional<Property> propertyOptional=propertyRepository.findById(property.getId());
        Property property1=propertyOptional.get();
        property.setJurisdiction(property1.getJurisdiction());
        property.setConfigId(property1.getConfigId());
        property.setGenre(property1.getGenre());
        propertyRepository.save(property);
        return property;
    }
    @RequestMapping("/deletese")
    public String deletese(Integer id){
        propertyListService.deleteByPropertyid(id);
        propertyRepository.deleteById(id);
        
        return "redirect:/prop";
    }
}
